﻿<?php

include_once('Common.php');

class UserController extends D_Controller {
	public $_ssRegisterInfo;
	private $_top;
	public $cralwer_obj;
	public $website_obj;
	public $crawl_website_url_obj;
	public $keyword_obj;
	public $data_obj;
	public $page_obj;
	
	function init() {
		set_time_limit(8889889);
		$this->setLayout ('main', '' );
		$this->user_obj = new User();
	}
	
	function registAction() {
		$this->noLayout ();
	}
	
	function loginAction() {
		$param = getParam('info');
		if($param != ""){
			$this -> noView();
			$info = jsonDecode($param,true);
			$validate = $this->user_obj -> validate_user($info['email'],$info['password']);
			if($validate){
				$user = new Zend_Session_Namespace('user');
				$user->email = $validate['email'];
				$user->phone = $validate['phone'];
				$user->full_name = $validate['last_name'] . " ". $validate['first_name'];
				$user->address = $validate['address'];
				$user->id = $validate['id'];
				echo  jsonEncode(array("status" => "success"));	
			}else{
				echo  jsonEncode(array("status" => "failure"));
			}
		}else{
			$this->noLayout();
		}
	}

	function logoutAction(){
		$this -> noView();
		$user = new Zend_Session_Namespace('user');
		Zend_Session::namespaceUnset('user');
		redirect(ROOT_URL);
	}
	function addregistAction(){
		$this -> noView();
		$param =  getParam('info');
		$info = jsonDecode($param,true);
		$arr_pass = password_hashed($info['password']);
		array_splice($info,1,1);
		$info= array_merge($info,$arr_pass);
		
		try{
			$userid = $this->user_obj -> checkAndInsert($info);
			if($userid){
				$user = new Zend_Session_Namespace('user');
				$user->email = $info['email'];
				$user->phone = $info['phone'];
				$user->full_name = $info['last_name'] . " ". $info['first_name'];
				$user->address = $info['address'];
				$user->id = $userid;
				echo  jsonEncode(array("status" => "success"));	
			}else{
				echo  jsonEncode(array("status" => "failure","msg" => "Exist Account"));
			}
		
		}
		catch(Exception $e){
			echo  jsonEncode(array("status" => "failure"));	
		}
		
	}
}

?>